package com.yuncheng.spcyApi.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.entity.SpcyXcjcJdzt;
import com.yuncheng.spcyApi.service.ISpcyXcjcJdztService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hua
 */
@Api(tags = "现场检查-内部结点接口")
@RestController
@RequestMapping("/api/spzx-spcy/xcjcjd")
public class SpcyXcjcJdztController {

    @Resource
    private ISpcyXcjcJdztService spcyXcjcJdztService;

    private static String jdList = "首次会议,现场检查笔记,检查报告制作,末次会议";

    /** 第一个状态 */
    public static String getFirstState(){
        return jdList.split(",")[0];
    }

    /** 下一个状态 */
    private static String getNextState(String jd){
        String nodeNameList[] = jdList.split(",");
        for (int i=0; i<nodeNameList.length; i++){
            if (nodeNameList[i].equals(jd)){
                if (i == nodeNameList.length-1){
                    return jd;
                }else {
                    return nodeNameList[i+1];
                }
            }
        }
        return "";
    }

    /** 上一个状态 */
    private static String getPreviousState(String jd){
        String[] nodeNameList = jdList.split(",");

        for (int i=0; i<nodeNameList.length; i++){
            if (nodeNameList[i].equals(jd)){
                if (i==0){
                    return nodeNameList[i];
                }else {
                    int perviousI = i-1; // 上一个环节名称
                    return nodeNameList[perviousI];
                }
            }
        }
        return "";
    }

    @Log(title = "查询现场检查当前结点")
    @ApiOperation(value = "查询现场检查当前结点")
    @GetMapping(value = "queryDqJdmc")
    public HttpResult queryDqJdmc(@RequestParam(value = "sqsxid", defaultValue = "") String sqsxid){
        if (StringUtils.isBlank(sqsxid)){
//            return HttpResultVo.HttpResultSave(this.getFirstState());
            return HttpResultVo.HttpResultGet("0");
        }

        List<SpcyXcjcJdzt> list = spcyXcjcJdztService.list(new LambdaQueryWrapper<SpcyXcjcJdzt>()
                .eq(SpcyXcjcJdzt::getFsqsxid, sqsxid));
        if (CollectionUtil.isNotEmpty(list)){
            return HttpResultVo.HttpResultGet(list.get(0).getFjdzt());
        }else {
            SpcyXcjcJdzt bean = new SpcyXcjcJdzt();
            bean.setId(GetUuIdUtils.NotReplaceUuId());
            bean.setFsqsxid(sqsxid);
//            bean.setFjdzt(this.getFirstState());
            bean.setFjdzt("0");
            spcyXcjcJdztService.save(bean);
        }

        return HttpResultVo.HttpResultGet("0");
    }

    @Log(title = "下一步")
    @ApiOperation(value = "下一步")
    @GetMapping(value = "nextNode")
    public HttpResult nextNode(@RequestParam(value = "sqsxid", defaultValue = "") String sqsxid,
                               @RequestParam(value = "fjdzt", defaultValue = "0") String fjdzt){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        /*List<SpcyXcjcJdzt> list = spcyXcjcJdztService.list(new LambdaQueryWrapper<SpcyXcjcJdzt>()
                .eq(SpcyXcjcJdzt::getFsqsxid, sqsxid));
        if (CollectionUtil.isNotEmpty(list)) {
            SpcyXcjcJdzt bean = list.get(0);
            bean.setFjdzt(this.getNextState(bean.getFjdzt()));
            spcyXcjcJdztService.updateById(bean);

            *//** 现场检查-给会议设置默认时间 *//*
            *//*if (bean.getFjdzt().equals("末次会议")){
                sqsxYwxxInfoService.setDefaultHysjBySqsxId(sqsxid, false, true);
            }*//*
        }*/

        LambdaUpdateWrapper<SpcyXcjcJdzt> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyXcjcJdzt::getFsqsxid, sqsxid);

        SpcyXcjcJdzt updateBean = new SpcyXcjcJdzt();
        updateBean.setFjdzt(fjdzt);
        spcyXcjcJdztService.update(updateBean, updateWrapper);

        return HttpResult.ok("操作成功");
    }

    @Log(title = "上一步")
    @ApiOperation(value = "上一步")
    @GetMapping(value = "previousNode")
    public HttpResult previousNode(@RequestParam(value = "sqsxid", defaultValue = "") String sqsxid,
                                   @RequestParam(value = "fjdzt", defaultValue = "0") String fjdzt){
        if (StringUtils.isBlank(sqsxid)){
            return HttpResult.error("事项参数为空！");
        }

        /*List<SpcyXcjcJdzt> list = spcyXcjcJdztService.list(new LambdaQueryWrapper<SpcyXcjcJdzt>()
                .eq(SpcyXcjcJdzt::getFsqsxid, sqsxid));
        if (CollectionUtil.isNotEmpty(list)) {
            SpcyXcjcJdzt bean = list.get(0);
            bean.setFjdzt(this.getPreviousState(bean.getFjdzt()));
            spcyXcjcJdztService.updateById(bean);
        }*/

        LambdaUpdateWrapper<SpcyXcjcJdzt> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyXcjcJdzt::getFsqsxid, sqsxid);

        SpcyXcjcJdzt updateBean = new SpcyXcjcJdzt();
        updateBean.setFjdzt(fjdzt);
        spcyXcjcJdztService.update(updateBean, updateWrapper);

        return HttpResult.ok("操作成功");

    }
}
